import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:homework_1/history.dart';
import 'package:homework_1/pay.dart';
import 'package:homework_1/tool_page.dart';
import 'package:homework_1/user_data.dart';
import 'package:homework_1/user_info.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'feedback.dart';


class UserHome extends StatefulWidget {
  @override
  _UserHomeState createState() => _UserHomeState();
}

class _UserHomeState extends State<UserHome> {
  Map<String, dynamic> userInfo = {};


  @override
  void initState() {
    super.initState();
    _loadUserInfo();
  }

  // Load user info from local storage
  Future<void> _loadUserInfo() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userInfoString = prefs.getString('userInfo');
    if (userInfoString != null) {
      setState(() {
        userInfo = jsonDecode(userInfoString);
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.white,
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              Card(
                // color: Colors.amber,
                child: Container(
                  height: 95,
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          CircleAvatar(
                              radius: 33,
                              backgroundImage: AssetImage('images/avatar/1.png')),
                          SizedBox(width: 16),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Text(
                                      userInfo['nickName'] ?? '昵称',
                                      style: TextStyle(
                                        fontSize: 16,
                                        color: Colors.black,
                                      ),
                                    ),
                                    SizedBox(width: 8),
                                  ],
                                ),
                                SizedBox(height: 4),
                                Row(
                                  children: [
                                    Text(
                                      userInfo['email'] ?? '邮箱',
                                      style: TextStyle(
                                        fontSize: 16,
                                        color: Colors.black,
                                      ),
                                    ),
                                    SizedBox(width: 8),
                                  ],
                                ),
                                SizedBox(height: 4),
                                Row(
                                  children: [
                                    Text(
                                      '余额: ',  // 这里是标题
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Colors.black,
                                      ),
                                    ),
                                    Text(
                                      userInfo['money'] != null ? (userInfo['money'] as num).toDouble().toStringAsFixed(2) : '0.00',
                                      // userInfo['money'].toString() ?? '0',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: Colors.red,
                                      ),
                                    ),
                                    SizedBox(width: 8),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          IconButton(
                            icon: Icon(Icons.arrow_forward_ios, color: Colors.black),
                            onPressed: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) => UserInfo()),
                              );
                            },
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(height: 16),
              buildMenuItem(Icons.favorite, '工具箱', ToolPage()),
              buildMenuItem(Icons.feedback, '反馈', FeedbackPage()),
              buildMenuItem(Icons.attach_money, '充值', RechargePage()),
              buildMenuItem(Icons.history, '历史会话', History()),
              buildMenuItem(Icons.data_exploration_rounded, '我的数据', UserData()),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildMenuItem(IconData icon, String text, Widget page) {
    return Card(
      color: Colors.white,
      child: ListTile(
        leading: Icon(
          icon,
          color: Colors.black,
        ),
        title: Text(
          text,
          style: TextStyle(
            fontSize: 16,
            color: Colors.black,
          ),
        ),
        trailing: Icon(
          Icons.arrow_forward_ios,
          color: Colors.black,
        ),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => page),
          );
        },
      ),
    );
  }
}
